package util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {

	private static final String CONFIG_PATH = "mybatis.xml";

	public static SqlSession getSession(){
		try {
			InputStream stream = Resources.getResourceAsStream(CONFIG_PATH);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
			return sqlSessionFactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


	public static void closeSession(SqlSession session) {
		if (session != null) {
			session.close();
		}
	}

}